<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Config;
use app\common\model\prize\Lists;
use app\common\model\score\Goods;
use app\common\model\score\Orders;
use app\common\model\ScoreLog;
use think\Db;
use think\Exception;

class Score extends Api
{
    protected $noNeedLogin = ["goods", "info"];
    protected $noNeedRight = "*";

    public function goods()
    {
        $size = $this->request->param("size", 10);
        $lists = (new Goods())->paginate($size);
        $this->success("", $lists);
    }

    public function info()
    {
        $id = $this->request->param("id", 0);
        $this->success("", Goods::get($id));
    }

    public function order()
    {
        $id = $this->request->param("id", 0);
        $remark = $this->request->param("remark", "");
        $good = Goods::get($id);
        if (!$good) {
            $this->error("当前商品有误");
        }

        if ($good['stock'] <= 0) {
            $this->error("库存不足");
        }
        Db::startTrans();
        try {
            $user = $this->auth->getUser();

            $order_sn = get_sn($this->auth->id);
            $data = [
                'order_sn' => $order_sn,
                'user_id' => $this->auth->id,
                'good_id' => $id,
                'name' => $good['name'],
                'cover' => $good['cover'],
                'type' => 1,
                'award_type' => $good['award_type'],
                'amount' => $good['amount'],
                'remark' => $remark,
                'score' => $good['score']
            ];
            if ($good['award_type'] == 0) {
                $data['status'] = 0;
            } else {
                $data['status'] = 1;
                \app\common\model\User::intersection($good, $user, "积分兑换", 6);
                if ($good['award_type'] == 3) {
                    $user->score = $user->score + $good['amount'];
                } else {
                    $key = Goods::getAwardType();
                    $user->$key = $user->$key + $good['amount'];
                }
            }

            $score_before = $user->score;
            $score_after = function_exists('bcadd') ? bcadd($user->score, -$data['amount'], 2) : $user->score - $data['amount'];
            ScoreLog::create([
                'user_id' => $this->auth->id, "score" => -$data['amount'],
                'before' => $score_before, 'after' => $score_after,
                'memo' => "积分兑换", 'type' => 2,
                'createtime' => time()
            ]);
            $good->setDec("stock");
            $user->score = $score_after;
            $user->save();
            \app\common\model\scroll\Notify::log($good['name'], $this->auth->nickname, 1);
            Orders::create($data);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        $this->success("");
    }

    public function order_lists()
    {
        $type = $this->request->param("type");
        $size = $this->request->param("size", 10);
        if (!$type) {
            $this->error("类型有误");
        }

        $this->success("", Orders::where(['user_id' => $this->auth->id, 'type' => $type])->paginate($size));
    }
}